package com.pkx.guide.task;

import com.pkx.guide.common.constant.Constants;
import com.pkx.guide.fcms.service.FcmsPlayService;
import com.pkx.guide.info.entity.TInfoGuide;
import com.pkx.guide.info.entity.TInfoGuidePublish;
import com.pkx.guide.info.service.TInfoGuidePublishService;
import lombok.extern.slf4j.Slf4j;

import java.util.Timer;
import java.util.TimerTask;

/**
 * 诱导屏自动发布
 *
 * @author sunsy
 * @since 2022-03-10
 */
@Slf4j
public class AutoPublishTimeTask extends TimerTask {

    /**
     * 发布接口
     */
    private TInfoGuidePublishService guidePublishService;

    /**
     * 设备控制接口
     */
    private FcmsPlayService fcmsPlayService;

    /**
     * 发布id
     */
    private Integer guidePublishId;

    /**
     * 情报板信息
     */
    private TInfoGuide guide;

    /**
     * 当前定时器
     */
    private Timer timer;

    public AutoPublishTimeTask(TInfoGuidePublishService guidePublishService, FcmsPlayService fcmsPlayService, Integer guidePublishId, TInfoGuide guide, Timer timer) {
        this.guidePublishService = guidePublishService;
        this.fcmsPlayService = fcmsPlayService;
        this.guidePublishId = guidePublishId;
        this.guide = guide;
        this.timer = timer;
    }

    @Override
    public void run() {
        if (this.guidePublishId == null || this.guidePublishService == null){
            this.timer.cancel();
            Constants.guidePublishTimerMap.remove(guidePublishId);
            return;
        }
        // 获取当前最新发布信息
        TInfoGuidePublish currentGuidePublish = this.guidePublishService.selectById(this.guidePublishId);
        // 当前发布任务已删除，或者更改为手动模式
        if (currentGuidePublish == null || currentGuidePublish.getDelFlg() == 1 || currentGuidePublish.getModeType() == 1){
            this.timer.cancel();
            Constants.guidePublishTimerMap.remove(guidePublishId);
            return;
        }
        log.info("自动发布执行：{}", guidePublishId);
        // 发布最新消息
        fcmsPlayService.fcmsActivePlayList(this.guide, currentGuidePublish.getPlayListName());
    }
}
